<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/8
 * Time: 13:46
 */
namespace app\blog\controller;
use cmf\controller\AdminBaseController;
use think\Db;

class OrderAdminController extends AdminBaseController
{
    //后台看流水   督导看自己管辖的店铺       总公司看所有
    public function index()
    {       
       /**搜索条件**/
       $param = $this->request->param();
       $where=array();
       $starttime=isset($param['start_time'])?$param['start_time']:'';
       $endtime=isset($param['end_time'])?$param['end_time']:'';
       $storeid=isset($param['storeid'])?$param['storeid']:'';
       $orderNo=isset($param['orderNo'])?$param['orderNo']:'';
       if(!empty($orderNo)){
           $where['orderNo']=$orderNo;
       }
       if(!empty($starttime) ) {
        	$where['ctime'][]=array('egt',strtotime($starttime));
       }
       if(!empty($endtime) ) {
        	$where['ctime'][]=array('lt',24*60*60+strtotime($endtime));
       }
       if(!empty($storeid) ) {
        	$where['storeid']=$storeid;
       }
       //判断登陆者权限
       $uid=session('ADMIN_ID');
       //$uid=2;
       $uinfo=Db::name('User')->where(array('id'=>$uid))->find();
       if($uinfo['user_type'] == 3){
           $store_list=Db::name('Store')->where(array('steer'=>$uid))->field('id')->select()->toarray();
           foreach($store_list as $k=>$v)
           {
                $new_list[] = $v['id'];
           }
           $string=implode(',',$new_list);
           $where['storeid']=array('in',$string);
       }
       $list=Db::name('Order')->where($where)->order("id DESC")->paginate(10);
       $list_store_list=Db::name('Store')->where(array('status'=>0))->select()->toarray();//正在运营的店铺列表
       // 获取分页显示
       $page = $list->render();
       $this->assign("list_store_list", $list_store_list);
       $this->assign("page", $page);
       $this->assign("list", $list);
       return $this->fetch();
    }
    public function importData()
    {
         $param = $this->request->param();
         //dump($param);exit;
         $map=array();
         if(!empty($param['orderNo']))///订单号不为空
         {
             $map['orderNo']=array('like','%'.$param['orderNo'].'%');
         }
         if(!empty($param['cardid']))///订单号不为空
         {
             $map['cardid']=array('like','%'.$param['cardid'].'%');
         }
         $starttime=isset($param['start_time'])?$param['start_time']:'';
         $endtime=isset($param['end_time'])?$param['end_time']:'';
         if(!empty($starttime) ) {            
             $map['ctime'][]=array('egt',strtotime($starttime));
         }
         if(!empty($endtime) ) {
             $map['ctime'][]=array('lt',24*60*60+strtotime($endtime));
         }
         $storeid=isset($param['storeid'])?$param['storeid']:'';
         if(!empty($storeid) ) {
             $map['storeid']=$storeid;
         }
         $list=Db::name('Order')->where($map)->order("id DESC")->select();
         //echo Db::name('Order')->getlastsql();exit;
         if(empty($list)){
            $this->error('暂无信息！');
         }
         $str="序号,订单号,会员,会员卡号,收银员,总价,实价,优惠金额,给钱,找零,付款类型,付款状态,店铺,时间\n";
         $str =iconv('utf-8','gb2312',$str);
        //循环记录构建逗号分隔的字段信息
        foreach($list as $row){
            $id=iconv('utf-8','gb2312',$row['id']);            
            $orderNo=iconv('utf-8','gb2312',$row['orderNo']);
            $member=iconv('utf-8','gb2312',get_member_data($row['userid'],'uname'));
            $cardid=iconv('utf-8','gb2312',$row['cardid']);
            $operatorid=iconv('utf-8','gb2312',get_operator_data($row['operatorid'],'user_login'));
            $totalMoney=iconv('utf-8','gb2312',$row['totalMoney']);
            $actualMoney=iconv('utf-8','gb2312',$row['actualMoney']);
            $discount=iconv('utf-8','gb2312',$row['discount']);
            $getMoney=iconv('utf-8','gb2312',$row['getMoney']);
            $giveChange=iconv('utf-8','gb2312',$row['giveChange']);
            $payType=iconv('utf-8','gb2312',get_pay_type($row['payType'],'pay_name'));
            $payStatus=iconv('utf-8','gb2312',get_pay_status($row['payStatus']));
            $storename=iconv('utf-8','gb2312',get_store_data($row['storeid'],'store_name'));
            $ctime=iconv('utf-8','gb2312',date('Y-m-d H:i:s',$row['ctime']));           
            $str.=$id.",".$orderNo.",".$member.','.$cardid.','.$operatorid.','.$totalMoney.','.$actualMoney.','.$discount.','.$getMoney.','.$giveChange.','.$payType.','.$payStatus.','.$storename.','.$ctime."\n"; //用引文逗号分开
        }
        $filename = date('YmdHis').'order.csv'; //设置文件名
        $this->exportCsv($filename,$str);
        exit;
    }
    public function exportCsv($filename, $data){
        header("Content-type:text/csv");
        header("Content-Disposition:attachment;filename=".$filename);
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
        header('Expires:0');
        header('Pragma:public');
        echo $data;
    }
    
    public function detail(){
        $param = $this->request->param();
        //dump($param);
        $id=$param['id'];
       // $goodsname=isset($param['goodsname'])?$param['goodsname']:'';       
//         if ($goodsname){
//             $where['g_goodsname']=['like',"%$goodsname%"];
//             $g_list=Db::name('Goods')->where($where)->field('g_id')->select()->toarray();
//             $g_id_arr=array();
//             foreach ($g_list as $k=>$v){
//                 $g_id_arr[]=$v['g_id'];
//             }
//             $g_id_str=implode(',',$g_id_arr);
//             $map['goodsid']=array('in',$g_id_str);
//         }
        $orderNo=get_order_data($id,'orderNo');
        $map['orderid']=$orderNo;
        //$list=Db::name('Order')->where($where)->order("id DESC")->paginate(10);
        //dump($orderNo);
        $detail_list=Db::name('OrderDetail')->where($map)->paginate(10);
        //echo Db::name('OrderDetail')->getlastsql();
        $page = $detail_list->render();
        $this->assign("detail_list", $detail_list);
        $this->assign("id", $id);
        $this->assign("page", $page);
        return $this->fetch();
    }
    
    
    
}